package a10_动态规划;

/**
 * <p>
 * a09_不同的二叉搜索树复习4
 * </p>
 *
 * @author flyduck
 * @since 2025/4/9
 */
public class a09_不同的二叉搜索树复习4 {

    //dp[3]
    //以1为根节点：dp[0] * dp[2]
    //以2为根节点：dp[1] * dp[1]
    //以3为根节点：dp[2] * dp[0]
    public int numTrees(int n) {
        if(n <= 2){
            return n;
        }
        int[] dp = new int[n+1];
        dp[0] = 1;
        dp[1] = 1;
        dp[2] = 2;

        for (int i = 3; i <= n; i++) {
            for (int j = 0; j < i; j++) {
                dp[i] = dp[i] + (dp[j] * dp[i-j-1]);
            }
        }

        return dp[n];
    }
}
